<?php
// $Id: matchdart.functions.inc 182 2010-03-13 17:49:35Z mwi.nor $

/**
 * @file
 * functions for the matchdart module.
 */

/**
 * _matchdart_get_eventselect
 *
 * Returns the matchdart_events for select boxes
 *
 * @package matchdart
 * @author mawi, <mailto://info@matchdart.de>
 * @param $optall Show an 'all' option value true/false
 * @return $events An option array containing the matchdart_events with fields:
 * value: nid
 * label: name
 * @see matchdart_block()
 */
function _matchdart_get_eventselect($optall = false) {
  $events = array();

  $sql = "SELECT nid, event_name FROM {matchdart_events} ORDER BY event_name";

  $sql = db_rewrite_sql($sql);
  $_events = db_query($sql);
  
  if ($optall) {
    $events[0] = t('- all -');
  };
  
  while ($event = db_fetch_object($_events)) {
    $events[$event->nid] = $event->event_name;
  }
  return $events;
}

/**
 * _matchdart_get_seasonselect
 *
 * Returns the matchdart_seasons for select boxes
 *
 * @package matchdart
 * @author mawi, <mailto://info@matchdart.de>
 * @param $optall Show an 'all' option value true/false
 * @return $seasons An option array containing the matchdart_seasons with fields:
 * value: nid
 * label: name
 * @see matchdart_block()
 */
function _matchdart_get_seasonselect($optall = false) {
  $seasons = array();

  $sql = "SELECT nid, season_name FROM {matchdart_seasons} ORDER BY season_name DESC";

  $sql = db_rewrite_sql($sql);
  $_seasons = db_query($sql);
  
  if ($optall) {
    $seasons[0] = t('- all -');
  };
  
  while ($season = db_fetch_object($_seasons)) {
    $seasons[$season->nid] = $season->season_name;
  }
  return $seasons;
}

/**
 * Returns the matchdart_players for select boxes
 *
 * @return $players An option array containing the matchdart_players with fields:
 * value: nid
 * label: name
 * @see matchdart_form()
 */
function _matchdart_get_playerselect() {
  $players = array();
  $players[0] = t('-none-');

  $sql = "SELECT nid, player_name FROM {matchdart_players} ORDER BY player_name";

  $sql = db_rewrite_sql($sql);
  $_players = db_query($sql);
  while ($player = db_fetch_object($_players)) {
    $players[$player->nid] = $player->player_name;
  }
  return $players;
}

/**
 * matchdart_sort_points
 *
 * Returns Array() sorted by 'points'
 *
 * @package matchdart
 * @author mawi, <mailto://info@matchdart.de>
 * @param $a, $b array rows to sort
 */
function _matchdart_sort_points($a, $b)
{
    if ($a['points'] == $b['points'])
    {
        return 0;
    } else
    {
        return ($a['points'] > $b['points']) ? -1 : 1;
    }
}
